
local guwang = fk.CreateSkill {
  name = "lingling__guwang",
  tags = { Skill.Compulsory },
}

Fk:loadTranslationTable{
  ["lingling__guwang"] = "顾望",
  [":lingling__guwang"] = "锁定技，你本回合获得的牌不计入手牌上限。轮次结束时，你可以使用一张你上回合获得的牌。",

  ["@@lingling__guwang-inhand-round"] = "顾望",
  ["#lingling__guwang-use"] = "顾望：你可以使用其中一张牌",
}

guwang:addEffect(fk.RoundEnd, {
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(guwang.name) and not player:isKongcheng()
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cards = table.filter(player:getCardIds("h"), function (id)
      return Fk:getCardById(id):getMark("@@lingling__guwang-inhand-round") > 0
    end)
    if #cards > 0 then
      room:askToPlayCard(player, {
        skill_name = guwang.name,
        pattern = tostring(Exppattern{ id = cards }),
        prompt = "#lingling__guwang-use",
        cancelable = true,
        extra_data = {
          bypass_times = true,
          extraUse = true,
        },
      })
    end
  end,
})
guwang:addEffect("maxcards", {
  exclude_from = function(self, player, card)
    return player:hasSkill(guwang.name) and card:getMark("@@lingling__guwang-inhand-round") > 0
  end,
})

guwang:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(guwang.name, true) and player.room.current == player then
      for _, move in ipairs(data) do
        if move.to == player and move.toArea == Card.PlayerHand then
          return true
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, move in ipairs(data) do
      if move.to == player and move.toArea == Card.PlayerHand then
        for _, info in ipairs(move.moveInfo) do
          if table.contains(player:getCardIds("h"), info.cardId) then
            room:setCardMark(Fk:getCardById(info.cardId), "@@lingling__guwang-inhand-round", 1)
          end
        end
      end
    end
  end,
})

guwang:addEffect(fk.TurnStart, {
  can_refresh = function (self, event, target, player, data)
    return target == player
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:clearHandMark(player, "@@lingling__guwang-inhand-round")
  end,
})

guwang:addLoseEffect(function (self, player, is_death)
  player.room:clearHandMark(player, "@@lingling__guwang-inhand-round")
end)

return guwang
